#include "qmlbook.h"

#include <QWebEngineProfile>
#include <QWebEngineScriptCollection>
#include <QWebEngineView>
#include <httpclient.h>
#include <qrcutil.h>

QmlBook::QmlBook()
{
    foreach (auto line, QR("://qml-book.txt").split("\n", Qt::SkipEmptyParts)) {
        auto key = line.split("\t").at(0);
        auto uri = line.split("\t").at(1);
        m_urls.insert(key, uri);
    }
}

QString QmlBook::name()
{
    return "中文版《QmlBook》";
}

QString QmlBook::prefix()
{
    return "https://qthub.com/static/doc/qmlbook/cn/";
}

QStringList QmlBook::searching(const QString &text)
{
    QStringList list;
    foreach (auto line, QR("://qml-book.txt").split("\n", Qt::SkipEmptyParts)) {
        auto key = line.split("\t").at(0);
        if (text.isEmpty() || key.contains(text, Qt::CaseInsensitive)) {
            list << key;
        }
    }
    return list;
}

void QmlBook::content(const QString &text, QWebEngineView *view)
{
    view->load(prefix() + m_urls.value(text));

    // QWebEngineProfile *profile = view->page()->profile();
    // QWebEngineScriptCollection *scripts = profile->scripts();
    // QString code("document.querySelector('body > div > div.book-summary').hidden = true;");
    // code.append("document.querySelector('body > div > div.book-body').setAttribute('style', 'left:0')");
    // QWebEngineScript self;
    // self.setSourceCode(code);
    // scripts->insert(self);
}
